package com.example.administrator.myapplication.evaluate;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myapplication.BmobApplication;
import com.example.administrator.myapplication.HomeActivity;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.tools.Advice;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * Created by Administrator on 2018/3/4.
 */

public class TestAssess extends Activity {
    private Toolbar toolbar;
    private String str;
    private BmobApplication apps;
    private Intent intent;
    private TextView tv_assess_score;
    private String score;
    private ImageView imageView;
    //用来判断是哪个方面的建议
    private int flag;
    //flag =1 heart  flag =2 muscle  flag =3 soft flag =4 body
    private TextView tv_assess_advice;
    private int criteria;


//    class MyTestHandler extends Handler {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            if (msg.what == 1) {
//                tv_assess_score.setText(score);
//            }
//        }
//    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_assess);
        apps = (BmobApplication) getApplication();
        tv_assess_score = (TextView) findViewById(R.id.tv_assess_score);
        imageView = (ImageView) findViewById(R.id.iv_testassess);
        tv_assess_advice = (TextView) findViewById(R.id.tv_assess_advice);

        Intent getintent = getIntent();
        Bundle bundle = getintent.getExtras();
        toolbar = (Toolbar) findViewById(R.id.toolbar_heard_assess);
        toolbar.setNavigationIcon(R.mipmap.custom_back);
        toolbar.setTitleMarginStart(140);
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));



        if (apps.getIntentflag() == 0) {
            switch (apps.getFlag()) {
                case 1:  //heart
                    str = "心肺功能测试结果";
                    score = apps.getHeart_result();
                    flag =1;
                    break;
                case 2:  //muscle
                    str = "肌力丶肌耐力测试结果";
                    score = apps.getMuscle_result();
                    flag =2;
                    break;
                case 3: //soft
                    str = "柔韧性体适能测试结果";
                    score = apps.getSoft_result();
                    flag =3;
                    break;
                case 4:
                    str = "身体成分测试结果";
                    score = apps.getBody_result();
                    flag =4;
                    break;
            }


        } else {
            switch (apps.getFlag()) {
                case 1:  //heart
                    str = "心肺功能测试结果";
                    score=bundle.getString("heart");
                    flag=1;
                    break;
                case 2:  //muscle
                    str = "肌力丶肌耐力测试结果";
                    score=bundle.getString("muscle");
                    flag =2;
                    break;
                case 3: //soft
                    str = "柔韧性体适能测试结果";
                    score=bundle.getString("soft");
                    flag=3;
                    break;
                case 4:
                    str = "身体成分测试结果";
                    score=bundle.getString("body");
                    flag=4;
                    break;
            }
        }
        criteria = Integer.parseInt(score);
        if (criteria < 15 && criteria >= 0) {
            imageView.setImageResource(R.mipmap.score_bg1);
        } else if (criteria < 30 && criteria >= 15) {
            imageView.setImageResource(R.mipmap.score_bg2);
        } else if (criteria < 45 && criteria >= 30) {
            imageView.setImageResource(R.mipmap.score_bg3);
        } else if (criteria < 55 && criteria >= 45) {
            imageView.setImageResource(R.mipmap.score_bg4);
        } else if (criteria < 70 && criteria >= 55) {
            imageView.setImageResource(R.mipmap.score_bg5);
        } else if (criteria < 85 && criteria >= 70) {
            imageView.setImageResource(R.mipmap.score_bg6);
        } else if (criteria <= 100 && criteria >= 85) {
            imageView.setImageResource(R.mipmap.score_bg7);
        }

        tv_assess_score.setText(score);

        //选择建议
        switch(flag){
            case 1:
                if (criteria >= 0 && criteria < 60) {
                    tv_assess_advice.setText(Advice.HEART_FIRST_ADVICE);
                } else if (criteria >= 60 && criteria < 90) {
                    tv_assess_advice.setText(Advice.HEART_SECOND_ADVICE);
                } else if (criteria > 90) {
                    tv_assess_advice.setText(Advice.HEART_THIRD_ADVICE);
                }
                break;
            case 2:
                if (criteria >= 0 && criteria < 60 && apps.is_man()) {
                    tv_assess_advice.setText(Advice.MAN_MUSCLE_FIRST);
                } else if (criteria >= 60 && criteria < 90 && apps.is_man()) {
                    tv_assess_advice.setText(Advice.MAN_MUSCLE_SECOND);
                } else if (criteria > 90 && apps.is_man()) {
                    tv_assess_advice.setText(Advice.MAN_MUSCLE_THIRD);
                } else if (criteria >= 0 && criteria < 60 && !apps.is_man()) {
                    tv_assess_advice.setText(Advice.WOMAN_MUSCLE_FIRST);
                } else if (criteria >= 60 && criteria < 90 && !apps.is_man()) {
                    tv_assess_advice.setText(Advice.WOMAN_MUSCLE_SECOND);
                } else if (criteria > 90 && !apps.is_man()) {
                    tv_assess_advice.setText(Advice.WOMAN_MUSCLE_THIRD);
                }
                break;
            case 3:
                if (criteria >= 0 && criteria < 60) {
                    tv_assess_advice.setText(Advice.SOFT_FIRST_ADVICE);
                } else if (criteria >= 60 && criteria < 90) {
                    tv_assess_advice.setText(Advice.SOFT_SECOND_ADVICE);
                } else if (criteria > 90) {
                    tv_assess_advice.setText(Advice.SOFT_THIRD_ADVICE);
                }
                break;
            case 4:
                if (criteria >= 0 && criteria < 60) {
                    tv_assess_advice.setText(Advice.BODY_FIRST_ADVICE);
                } else if (criteria >= 60 && criteria < 90) {
                    tv_assess_advice.setText(Advice.BODY_SECOND_ADVICE);
                } else if (criteria > 90) {
                    tv_assess_advice.setText(Advice.BODY_FIRST_ADVICE);
                }
                break;
        }
        tv_assess_advice.setMovementMethod(new ScrollingMovementMethod());

        toolbar.setTitle(str);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (apps.getIntentflag() == 0) {
                    intent = new Intent(TestAssess.this, HomeActivity.class);
                    startActivity(intent);
                    finish();
                } else if (apps.getIntentflag() == 1) {
                    finish();
                }


            }
        });
    }
  /*  class Mythread extends Thread {
        public String str;

        public Mythread(String str) {
            this.str = str;
        }

        @Override
        public void run() {
            query(str);
        }

    }

    public void query(final String str) {
        //获取objectid
        File file = new File(getFilesDir(), "objectid.txt");
        String objectid = null;
        String result = null;
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            objectid = bufferedReader.readLine();
            bufferedReader.close();
            BmobQuery query = new BmobQuery("_User");
            query.getObjectByTable(objectid, new QueryListener<JSONObject>() {
                @Override
                public void done(JSONObject jsonObject, BmobException e) {
                    try {
                        score = jsonObject.getString(str);
                        MyTestHandler handler = new MyTestHandler();
                        handler.sendEmptyMessage(1);

                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }*/
}





